package com.aji.biomarker.controller;

import com.aji.allcommon.result.Result;
import com.aji.biomarker.domain.Compound;
import com.aji.biomarker.domain.dto.CompoundConditionDto;
import com.aji.biomarker.domain.dto.CompoundDto;
import com.aji.biomarker.domain.dto.SpeciesDto;
import com.aji.biomarker.service.CompoundService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "化合物管理相关接口")
@RestController
@RequestMapping("/comp")
@RequiredArgsConstructor
@CrossOrigin
public class CompoundController {
    private final CompoundService compoundService;
    @ApiOperation("增加 需要忽略id")
    @PostMapping()
    public Result save(@RequestBody Compound entity){
        compoundService.save(entity);
        return Result.success();
    }
    @ApiOperation("删除")
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        compoundService.removeByIds(ids);
        return Result.success();
    }
    @ApiOperation("修改")
    @PutMapping()
    public Result updateById(@RequestBody Compound entity){
        System.out.println(entity);
        compoundService.updateById(entity);
        return Result.success();
    }
    @ApiOperation("查询 根据id")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.success(compoundService.getById(id));
    }

    @ApiOperation("查询 全部")
    @GetMapping("/list")
    public Result list(){
        return Result.success(compoundService.listDistinct());
    }

    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{pageSize}")
    public Result listByConditionAndPage(@PathVariable int page, @PathVariable int pageSize, CompoundConditionDto compound){
        //合法检验
        if (page < 1 || pageSize < 1) {
            return Result.error("分页参数异常");
        }

        Page<Compound> compoundPage = new Page<>(page, pageSize);

        QueryWrapper<Compound> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT cname", "ename", "formula", "cas", "weight"); // 整行去重可选字段
        if (compound.getCname() != null && !compound.getCname().isEmpty()) {
            wrapper.like("cname", compound.getCname());
        }
        if (compound.getEname() != null && !compound.getEname().isEmpty()) {
            wrapper.like("ename", compound.getEname());
        }
        if (compound.getFormula() != null && !compound.getFormula().isEmpty()) {
            wrapper.like("formula", compound.getFormula());
        }
        if (compound.getCas() != null && !compound.getCas().isEmpty()) {
            wrapper.like("cas", compound.getCas());
        }
        if (compound.getWeightMin() != null) {
            wrapper.ge("weight", compound.getWeightMin());
        }
        if (compound.getWeightMax() != null) {
            wrapper.le("weight", compound.getWeightMax());
        }

        IPage<Compound> compoundIPage = compoundService.page(compoundPage, wrapper);

        return Result.success(compoundIPage);
    }

    @ApiOperation("数据统计")
    @GetMapping("/count")
    public Result<List<CompoundDto>> getOriginCounts() {
        try {
            List<CompoundDto> counts = compoundService.countByCname();
            return Result.success(counts);
        } catch (Exception e) {
            return Result.error("获取数据统计失败: " + e.getMessage());
        }
    }
}
